package com.kfm.login.util;

import com.kfm.login.spring.SpringContextUtils;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

public class MailUtil {

    public static void sendMail(String to, String subject, String body) throws MailException {
        JavaMailSender mailSender = SpringContextUtils.getBean(JavaMailSender.class);
        String from = SpringContextUtils.getVal("spring.mail.username");

        // 发送html邮件
        MimeMessagePreparator messagePreparator = mimeMessage -> {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
            messageHelper.setFrom(from);
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setText(body,true);
        };
        mailSender.send(messagePreparator);
    }

    public static void sendActiveMail(String to, String activeUrl) throws MailException {
        String subject = "激活邮件";
        String body = "您的激活地址是: <a href='" + activeUrl + "'>激活地址</a>。链接 5 分钟内有效，请尽快激活！";
        sendMail(to, subject, body);
    }
}
